<template>
  <div class="login-container">
    <div class="login-box">
      <h2>用户登录</h2>
      <div class="box2">
        <el-form :model="loginForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="账号" prop="username">
            <el-input type="text" v-model="loginForm.username" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="loginForm.password" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
        <div class="notice">
          notice : 账号:admin 密码:123456
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapMutations ,mapState} from 'vuex'


export default {
  name: 'LoginView',
  data() {
    var validateUsername = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入账号'));
      } else {
        callback();
      }
    };
    var validatePassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        callback();
      }
    };
    return {
      loginForm: {// 表单数据
        username: '',
        password: '',
      },
      rules: {
        username: [// 验证规则
          { validator: validateUsername, trigger: 'blur' }
        ],
        password: [
          { validator: validatePassword, trigger: 'blur' }
        ],
      }
    };
  },
  computed: {
    ...mapState('user', ['redirectPath']) // ✅ 添加这一行
  },
  methods: {
    ...mapMutations('user', ['setUserInfo']),
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if (this.loginForm.username === 'admin' && this.loginForm.password === '123456') {
            let userInfo = {
              username: this.loginForm.username,
              isLogin: true,
              token: '123456'
            }

            this.setUserInfo(userInfo)
            localStorage.setItem('userInfo', JSON.stringify(userInfo))

            this.$message.success('登录成功')

            // ✅ 使用 computed 中的 redirectPath
            const redirect = this.redirectPath || '/'
            console.log('redirectPath:', this.redirectPath)
            this.$router.push(redirect)
          } else {
            this.$message.error('用户名或密码错误')
          }
        } else {
          this.$message.error('表单校验失败，请检查输入内容')
          return false
        }
      })
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: url('@/assets/images/login-bg.jpg') no-repeat center;
  background-size: cover;
}

.login-box {
  width: 400px;
  padding: 30px;
  background-color: #fff;
  border-radius: 10px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);

  .box2 {
    margin-left: -20px;
    margin-right: 50px;
  }

  .notice {
    color: #999;
    font-size: 12px;
    text-align: left;
    padding-left: 20px;
    margin-top: 20px;
  }

}

.login-box h2 {
  text-align: center;
  margin-bottom: 20px;
  color: #333;
}
</style>